<template>
<div>
  <div class="search-bar" :class="{'hide-title':!titleVisible,'hide-shadow':!shadowVisible}">
      <transition  name='title-move'>
    <div class="search-bar-title-wrapper" v-show="titleVisible">
     
      <div class="title-text-wrapper">
        <span class="title-text title">
          {{ $t("home.title") }}
        </span>
      </div>
      <div class="title-icon-shake-wrapper" @click="showFlapCard">
        <span class="iconfont icon-shake icon"></span>
      </div>
    </div>
    </transition>
     <div class="title-icon-back-wrapper" :class="{'hide-title':!titleVisible}" @click="back">
        <span class="iconfont icon-zuojiantou icon"></span>
      </div>
    <div class="search-bar-input-wrapper" :class="{'hide-title':!titleVisible}">
        <div class="search-bar-blank" :class="{'hide-title':!titleVisible}"></div>
      <div class="search-bar-input">
        <span class="iconfont icon-search icon"></span>
        <input
          type="text"
          :placeholder="$t('home.hint')"
          v-model="searchText"
          @keyup.13.exact="search"
          @click="showHotsearch"
        />
      </div>
    </div>
  </div>
  <hot-search-list v-show="hotSearchVisible" ref="hotSearch"></hot-search-list>
  </div>
</template>

<script>
import { storeHomeMixin } from "../../utils/mixin";
import HotSearchList from './HotSearchList.vue';
export default {
  components: { HotSearchList },
  mixins: [storeHomeMixin],
  watch:{
      offsetY(offsetY){
          if(offsetY>0){
              this.hideTitle()
              this.showshadow()
          }else{
              this.showTitle()
              this.hideshadow()
          }
       
      },
       hotSearchOffsetY(offsetY){
            if(offsetY>0){
                this.showshadow()
            }else{
                this.hideshadow()
            }
        }
  },
  data() {
    return {
      searchText: "",
      titleVisible: true,
      shadowVisible:false,
      hotSearchVisible:false
    };
  },
  methods:{
    search(){
     this.$router.push({
       path:'/store/list',
       query:{
         keyword:this.searchText
       }
     })
    },
      showFlapCard(){
          this.setFlapCardVisible(true)
      },
      back(){
          if(this.offsetY>0){
              this.showshadow()
          }else{
              this.hideshadow()
          }
          this.showTitle()
          this.hideHotsearch()
      },    
      showHotsearch(){
          this.hideTitle()
          this.hideshadow()
          this.hotSearchVisible=true
          this.$nextTick(()=>{
              this.$refs.hotSearch.reset()
          })
      },
       hideHotsearch(){
            this.hotSearchVisible=false
           if(this.offsetY>0){
               this.hideTitle()
               this.showshadow()
           }else{
               this.showTitle()
               this.hideshadow()
           }
        
      },
      hideTitle(){
          this.titleVisible=false
      },
      showTitle(){
          this.titleVisible=true
      },
       hideshadow(){
          this. shadowVisible=false
      },
      showshadow(){
          this. shadowVisible=true
      }
  }
};
</script>

<style lang="scss" scoped>
@import "../../assets/styles/global.scss";
.search-bar {
  position: relative;
  width: 100%;
  height: px2rem(94);
  z-index: 150;
  box-shadow: 0 px2rem(2) px2rem(2) 0 rgba(0, 0, 0, .1);
  &.hide-title{
      height: px2rem(52);
  }
  &.hide-shadow{
      box-shadow: none;
  }
  .search-bar-title-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: px2rem(42);

    .title-text-wrapper {
      width: 100%;
      @include center;
      height: px2rem(42);
    }
    .title-icon-shake-wrapper {
      position: absolute;
      right: px2rem(15);
      top: 0;
      height: px2rem(42);
      @include center;
    }
  }
      .title-icon-back-wrapper {
      position: absolute;
      left: px2rem(15);
      top: 0;
      z-index: 100;
      height: px2rem(42);
      @include center;
       transition: all $animationTime $animationType;
       &.hide-title{
      height: px2rem(52);
  }
    }
  .search-bar-input-wrapper {
    display: flex;
    width: 100%;
    height: px2rem(52);
    padding: px2rem(5) px2rem(15);
    box-sizing: border-box;
    position: absolute;
    top: px2rem(42);
    left: 0;
   transition: all $animationTime $animationType;
     &.hide-title{
     top:0
  } 
  .search-bar-blank{
      width: 0;
      flex: 0 0 0;
      transition: all $animationTime $animationType;
    &.hide-title{
    width: px2rem(31);
      flex: 0 0 px2rem(31);
      
    }
  }
    .search-bar-input {
        flex: 1;
      border-radius: px2rem(20);
      width: 100%;
      background: #f4f4f4;
      @include left;
      padding: px2rem(10);
      box-sizing: border-box;
      border: px2rem(1) solid #eee;
      .icon-search {
        color: #999;
      }
      input {
        width: 100%;
        height: px2rem(22);
        border: none;
        background-color: transparent;
        margin-left: px2rem(10);
        font-size: px2rem(12);
        color: #666;
        &:focus {
          outline: none;
        }
        &::-webkit-input-placeholder {
          color: #ccc;
        }
      }
    }
  }
}
</style>
